package multithreadedgui.model;

import multithreadedgui.util.CollectionsUtil;

import java.util.List;

/**
 * Use at own risk.
 * Creation data:
 * User: Alex
 * Date: 12/04/12
 * Time: 19:55
 */
public class DemoModel {
    private PiTask piTask;
    private double pi;
    private List<PropertyChangeListener> propertyChangeListeners = CollectionsUtil.makeList();

    public DemoModel() {}

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeListeners.add(listener);
    }

    public void calculatePi() {
        piTask = new PiTask(new Callback<Double>() {
            @Override
            public void call(Double result) {
                setPi(result);
            }
        });
        piTask.execute();
    }

    public double getPi() {
        return pi;
    }

    public void setPi(double pi) {
        this.pi = pi;
        for(PropertyChangeListener listener: propertyChangeListeners)
            listener.notifyPropertyChanged("pi");
    }
}
